<!doctype html>
<html>
	<head>
		<title>忘记密码</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, width=device-width">
		<meta name="format-detection" content="telephone=no">
		<link rel="stylesheet" href="assets/css/amazeui.min.css">
		<link rel="stylesheet" href="assets/css/style.css">
	</head>
	<body class="body">
		<div class="containter">
			<header data-am-widget="header" class="am-header am-header-default">
				<div class="am-header-left am-header-nav">
					<a href="javascript:;" onClick="javascript :history.back(-1);">
						<img src="assets/images/left.png">
					</a>
				</div>
				<h1 class="am-header-title" style="margin:0 14%">
      忘记密码
    </h1>
			</header>
			<div style="height: 4rem; width: 100%;"></div>
			<div class="change">
				<form action="">
					<ul>
						<li class="zh">
							<input id="phonenumber" type="text" placeholder="请输入您的手机号">
						</li>
						<li class="yz">
							<input id="verification_code" type="text" placeholder="请输入短信验证码">
							<button type="button" class="getcode">获取短信验证码</button>
						</li>
						<li>
							<input id="pass_word" type="password" placeholder="请输入您的密码">
							<a href="javascript:;" class="close"></a>
						</li>
						<li>
							<input id="check_password" type="password" placeholder="请再次输入您的密码">
							<a href="javascript:;" class="close"></a>
						</li>
					</ul>
					<button class="getpassword" type="button">找回并修改密码</button>
				</form>
			</div>
		</div>
		<div id="toast" style="top:-11.5rem"></div>
		<script type="text/javascript" src="assets/js/jquery-1.9.0.min.js"></script>
		<script src="assets/js/jquery.md5.js"></script>
		<script src="assets/js/allUse.js"></script>
		<script type="text/javascript">
			var rxPhone = /^1[3|4|5|7|8][0-9]{9}$/;
			var rxpassword = /^[a-zA-Z0-9]{6,18}$/;
			$(function() {
				//登录页面-密码显示
				$(".close").click(function() {
					$(this).toggleClass("close");
					$(this).toggleClass("key");
					if($(this).is('.key')) {
						$(this).parents("li").find("input").attr("type", "text");
					} else {
						$(this).parents("li").find("input").attr("type", "password");
					}
				});
			});
			//5.4忘记密码
			var flagthis=true
			$(".getpassword").click(function() {
				var phone_num = $.trim($("#phonenumber").val());
				var verification_code = $.trim($("#verification_code").val());
				var pass_word =$.md5($.md5($.trim($("#pass_word").val())));
				
				if(!phone_num){
					showToast("请输入手机号")
					return;
				}else if(!rxPhone.test(phone_num)){
					showToast("手机号码有误");
					return;
				}else if(!verification_code){
					showToast("请输入验证码");
					return;
				}else if(!rxpassword.test($.trim($("#pass_word").val()))){
					showToast("密码长度应为6-18位并且为字母数字组合");
					return;
				}else if($.trim($("#pass_word").val())!==$.trim($("#check_password").val())){
					showToast("两次密码不一致");
					return;
				}else if(!flagthis){
					return;
				}
				flagthis=false
				$(function() {
					$.ajax({
						type: "post",
						url:url_enter+"/api/reset_pwd_by_mobile",
						headers: {
							"Os":ios_Andriod,
							"Device-Id":"",
							"Screen":wh,
							"Content-Type": "application/x-www-form-urlencoded"
						},
						data: {
							"phone_num": phone_num, //注册时手机号，字符串类型（String）
							"verification_code": verification_code, //短信验证码，字符串类型（String）
							"password": pass_word //用户新密码，未加密，字符串类型（String）
						},
						success: function(data, status, xhr) {
							flagthis=true;
							console.log(data);
							if(data.result==1){
								alert(data.message);
								sessionStorage.setItem("forgeturl",window.location.href);
								window.location.href="login.html";
							}else{
								alert(data.message)
							}
						},
						error: function(xhr, status, thrown) {
							console.log(xhr);
						}
					});
				});
			});
			$("input").on('input propertychange',function(){
				if($("#phonenumber").val() && $("#verification_code").val() && $("#pass_word").val() && $("#check_password").val()===$("#pass_word").val()){//表单全部填过了
					$(".getpassword").addClass("turn-pink");
				}else{
					$(".getpassword").removeClass("turn-pink");
				};
			});
			var flg=true;
			function calc(){//倒计时
				var initTime=60;
				var timer=setInterval(function(){
					initTime--
					if(initTime===0){
						showinner(false,initTime)
						flg=true
						clearInterval(timer)					
					}else{
						flg=false
						showinner(true,initTime)
					}
					
				},1000)
			};
			function showinner(flag,time){
				if(flag){
					$(".getcode").html(time)
				}else{
					$(".getcode").html("获取短信验证码")
				}
			};
			
			//5.2获取验证码
			$('.getcode').click(function() {
				var phone_num = $.trim($(" #phonenumber ").val());
				if(!phone_num){
					showToast("请输入手机号")
					return;
				}else if(!rxPhone.test(phone_num)){
					showToast("手机号码有误");
					return;
				}else if(flg){
					getCode(phone_num)
				}
					
			});
			function getCode(phone_num){
				$.ajax({
						url: url_enter+"/api/get_sms_verify_code",
						type: "get",
						data: {
							"phone_num": ""+phone_num+"", //注册手机号，字符串类型（String）
							"types":"reset"
						},
						success: function(data, status, xhr) {
							 calc();
							if(data.result!==1){
								alert(data.message)
							}else{
								showToast("验证码已发送，请注意查收");
							}
						},
						error: function(xhr, status, thrown) {
							console.log(xhr);
						}
					});
			}
		</script>
	</body>

</html>